package com.spzx.product.controller;

import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.BrandService;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author lynn
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/productUnit")
@Tag(name = "商品单位")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService  productUnitService;


    /**
     * 查询商品单位列表
     * @param name
     * @return
     */
    @RequiresPermissions("base:productUnit:list")
    @GetMapping("/list")
    @Operation(summary = "查询商品单位",description = "根据名称查询商品单位")
    public TableDataInfo list(@RequestParam(required = false) String name){
        startPage(); //分页
        List<ProductUnit> list;
        if(StringUtils.hasText(name)){
            list  = productUnitService.selectProductUnitListByName(name);
        }else {
            list = productUnitService.list();
        }
        return getDataTable(list);
    }


    /**
     * 删除商品单位
     */
    @RequiresPermissions("base:productUnit:remove")
    @Log(title = "商品单位管理", businessType = BusinessType.DELETE)
    @Operation(summary = "删除商品单位",description = "根据商品单位id删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(name = "ids", description = "商品单位id数组")
            @PathVariable Long[] ids)
    {
        return toAjax(productUnitService.removeBatchByIds(List.of(ids)));
    }

    /**
     * 修改商品单位
     */
    @RequiresPermissions("base:productUnit:edit")
    @Log(title = "商品单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改商品单位")
    public AjaxResult edit(@Validated @RequestBody ProductUnit productUnit)
    {
        if (!productUnitService.checkProductUnitNameUnique(productUnit))
        {
            return error("修改商品单位'" + productUnit.getName() + "'失败，商品单位名称已存在");
        }
        productUnit.setUpdateBy(SecurityUtils.getUsername());//设置修改人
        return toAjax(productUnitService.updateById(productUnit));
    }

    /**
     * 新增商品单位
     */
    @RequiresPermissions("base:productUnit:add")
    @Log(title = "商品单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增商品单位")
    public AjaxResult add(@Validated @RequestBody ProductUnit productUnit)
    {
        if (!productUnitService.checkProductUnitNameUnique(productUnit))
        {
            return error("新增商品单位'" + productUnit.getName() + "'失败，商品单位名称已存在");
        }
        productUnit.setCreateBy(SecurityUtils.getUsername());//设置新增人
        return toAjax(productUnitService.save(productUnit));
    }

    /**
     * 获取商品单位详情
     */
    @Operation(summary = "获取商品单位详细信息")
    @RequiresPermissions("base:productUnit:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(productUnitService.getById(id));
    }


    @Operation(summary = "获取商品单位列表")
    @GetMapping("/getUnitAll")
    public AjaxResult getProductUnitAll()
    {
        List<ProductUnit> list = productUnitService.list();
        return AjaxResult.success(list);
    }
}
